package com.umfwechat.util;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.umfwechat.common.JsonAble;
import com.umfwechat.common.code.RetCode;

//import com.umfWeChat.common.JsonAble;
//import com.umfWeChat.common.code.RetCode;



public class SaasJsonUtil {
	
	private static Logger logger = LoggerFactory.getLogger(SaasJsonUtil.class);

	private static final String INFTYPE_TURN = "skip";
	private static final String INFTYPE_INIT = "init";
	private static final String INFTYPE_BUSI= "busi";
	/*
	 * 跳转接口json拼接
	 * ***/
	public static String makeSkipJsonMsg(String url){
		StringBuilder str=new StringBuilder();
		str.append("{");
		str.append("\"success\":"+true+",");//成功标识
		str.append("\"nextUrl\":\""+url+"\",");//需要跳转页面的路径
		str.append("\"infType\":\""+INFTYPE_TURN+"\"");//接口类型
		str.append("}");
		return str.toString();
	}
	/*
	 * 初始化接口json拼接
	 * ***/
	public static String makeInitJsonMsg(Map<String,Object> map,String retCode,String retMsg){
		StringBuilder str=new StringBuilder();
		str.append("{");
		str.append("\"success\":"+true+",");//成功标识
		str.append("\"retCode\":\""+retCode+"\",");//返回码
		str.append("\"retMsg\":\""+retMsg+"\",");//返回描述
//		str.append("\"nextUrl\":\""+""+"\",");//需要跳转页面的路径
		if(map!=null){str.append(makeCommJsonMsg(map));}  //拼接MAP字符串
		str.append("\"infType\":\""+INFTYPE_INIT+"\"");//接口类型
		str.append("}");
		return str.toString();
	}
	//
	public static String makeInitJsonMsg(Map<String,Object> map){
		return makeInitJsonMsg(map,RetCode.SUCCESS,"成功");
	}
	
	public static String makeInitSuccJsonMsg(){
		return makeInitJsonMsg(null,RetCode.SUCCESS,"成功");
	}
	//业务接口失败调用
	public static String makeInitErrJsonMsg(String retCode,String retMsg){
		return makeBusiJsonMsg(null,retCode,retMsg);
	}
	/*
	 * 业务功能接口json拼接
	 * ***/
	public static String makeBusiJsonMsg(Map<String,Object> map,String retCode,String retMsg){
		StringBuilder str=new StringBuilder();
		str.append("{");
		str.append("\"success\":"+true+",");//成功标识
		str.append("\"retCode\":\""+retCode+"\",");//返回码
		str.append("\"retMsg\":\""+retMsg+"\",");//返回描述
		if(map!=null){str.append(makeCommJsonMsg(map));}  //拼接MAP字符串
		str.append("\"infType\":\""+INFTYPE_INIT+"\"");//接口类型
		str.append("}");
		return str.toString();
	}
	//业务接口成功调用
	public static String makeBusiJsonMsg(Map<String,Object> map){
		return makeBusiJsonMsg(map,RetCode.SUCCESS,"成功");
	}
	
	public static String makeBusiSuccJsonMsg(){
		return makeBusiJsonMsg(null,RetCode.SUCCESS,"成功");
	}
	//业务接口失败调用
	public static String makeBusiErrJsonMsg(String retCode,String retMsg){
		return makeBusiJsonMsg(null,retCode,retMsg);
	}
	/*
	 * map转json字符串（子功能方法，不可直接调用）
	 * ***/
	@SuppressWarnings("unchecked")
	public static String makeCommJsonMsg(Map<String,Object> map){
		StringBuilder str=new StringBuilder();
		for (String key : map.keySet()) {
			Object item=map.get(key);
			if(item==null){//空指针化为空串
				item="";
			}
			if(item instanceof String){
				str.append("\""+key+"\":\""+item+"\",");//拼接字符串		
				continue;
//			}else if(item instanceof Integer||item instanceof Double||item instanceof Float||item instanceof Long){
			}else if(item instanceof Number){
				str.append("\""+key+"\":"+item+",");//拼接字符串
				continue;
			}else if(item instanceof Boolean){
				str.append("\""+key+"\":"+item+",");//拼接字符串
				continue;
			}else if(item instanceof Date){
				str.append("\""+key+"\":\""+item.toString()+"\",");//拼接字符串
				continue;
			}else if(item instanceof JsonAble){
//				Class<JsonAble> clas;
				try {
//					clas = (Class<JsonAble>) Class.forName(item.getClass().getName());
//					JsonAble ja = clas.newInstance();
					JsonAble jia = (JsonAble)item;
					str.append("\""+key+"\":"+jia.toJsonString()+",");//拼接字符串	
					continue;
				} catch (Exception e) {
					logger.error("自定义对象未实现JsonAble接口",e);
				}
			}else if(item instanceof List){
				str.append("\""+key+"\":"+makeListJsonMsg((List)item)+",");
				continue;
			}else{
				str.append("\""+key+"\":\"\",");//拼接字符串	
				continue;
			}
		}
		return str.toString();
		
	} 
	
	/*
	 * list转json字符串（子功能方法，不可直接调用）
	 * ***/
	public static <T extends JsonAble> String makeListJsonMsg(List<T> list){
		StringBuilder str=new StringBuilder();
		if(list==null||list.size()==0){
			str.append("[]");
			return str.toString();
		}else{
			str.append("[");
			for (T t : list) {
				if(t instanceof List){
					str.append(makeListJsonMsg((List)t)+",");
				}else if(t instanceof Map){
					str.append("{");
					StringBuffer tmp=new StringBuffer(makeCommJsonMsg((Map<String,Object>)t));
					str.append((tmp.length()>0)?tmp.deleteCharAt(tmp.length()-1).toString():tmp.toString());
					str.append("},");
				}else{
					Method[] a =t.getClass().getMethods();
					for (Method method : a) {
						if(method.toString().contains("toJsonString")){
							try {
								str.append(t.toJsonString()+",");
							} catch (Exception e) {
								logger.error("自定义对象未实现JsonAble接口",e);
							}//拼接字符串	
						}
					}
					
					continue;
				}
			}
			if(str.length()>1){
				str.deleteCharAt(str.length()-1);//去掉最后一个逗号
			}
			str.append("]");
			return str.toString();
		}
	}
	public static String mapToObjectStr(Map<String,String> map){
		StringBuilder str=new StringBuilder();
		str.append("{");
		for (String key : map.keySet()) {
			str.append("\""+key+"\":\""+map.get(key)+"\",");//拼接字符串
		}
		if(str.length()>1){
			str.deleteCharAt(str.length()-1);//去掉最后一个逗号
		}
		str.append("}");
		return str.toString();
	}
}
